package com.wdb007.venus.svc.biz.book;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.wdb007.venus.svc.biz.manager.ConfigInfoManager;
import com.wdb007.venus.svc.biz.utils.DateUtil;
import com.wdb007.venus.svc.dal.order.dao.OrderTransDAO;
import com.wdb007.venus.svc.dal.user.model.UserBorrowListDO;
import com.wdb007.venus.svc.dal.user.model.UserBorrowPageDO;
import com.wdb007.venus.svc.facade.BookInfoService;
import com.wdb007.venus.svc.facade.book.model.BorrowedHistDTO;
import com.wdb007.venus.svc.facade.book.model.BorrowedHistVO;
import com.wdb007.venus.svc.facade.book.model.UserBorrowPageDTO;
import com.wdb007.venus.svc.facade.constant.RspCodeEnum;
import com.wdb007.venus.svc.facade.exception.BizException;

@Service("bookInfoService")
public class BookInfoServiceImpl implements BookInfoService {

	@Autowired
	private OrderTransDAO orderTransDAO;
	@Autowired
	private ConfigInfoManager configInfoManager;
	
	private String getCostFlagRemark(Integer costFlag) {
		if (1 == costFlag) {
			return "超额借阅2元/天";
		} else if (2 == costFlag) {
			return "包月期间借阅免费";
		} else if (3 == costFlag) {
			return "包年期间借阅免费";
		}
		return "";
	}

	@Override
	public BorrowedHistVO getBorrowList(UserBorrowPageDTO userBorrowPageDTO) throws BizException {

		UserBorrowPageDO pageDO = new UserBorrowPageDO();
		BeanUtils.copyProperties(userBorrowPageDTO, pageDO);

		BorrowedHistVO vo = new BorrowedHistVO();
		int count = orderTransDAO.countBorrowedList(pageDO);
		vo.setTotalCount(count);
		List<UserBorrowListDO> list = orderTransDAO.getBorrowedList(pageDO);
		if (CollectionUtils.isEmpty(list))
			throw new BizException(RspCodeEnum.QUERY_LIST_IS_NULL);

		DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm");
		for (UserBorrowListDO bl : list) {
			BorrowedHistDTO dto = new BorrowedHistDTO();
			if (null == bl.getAuthor()) {
				dto.setAuthor("暂无");
			} else {
				dto.setAuthor(bl.getAuthor());
			}
			if (null == bl.getBookName()) {
				dto.setBookname("暂无");
			} else {
				dto.setBookname(bl.getBookName());
			}
			if (null == bl.getImgurl()) {
				dto.setImgurl(configInfoManager.getBookDefaultImg());
			} else {
				dto.setImgurl(configInfoManager.getImgurl(bl.getImgurl()));
			}
			dto.setCostFlag(bl.getCostFlag());
			dto.setCostFlagRemark(getCostFlagRemark(bl.getCostFlag()));
			dto.setStarttime(df.format(bl.getCreateTime()));
			if ("1".equals(userBorrowPageDTO.getOper()) || "2".equals(userBorrowPageDTO.getOper()))
				dto.setReturntime(df.format(bl.getModifyTime()));
			dto.setIsbn(bl.getIsbn());
			BigDecimal bd = (null == bl.getTransAmount()) ? new BigDecimal("0.00") : bl.getTransAmount();
			dto.setCost("¥" + bd.setScale(2).toString());
			dto.setBorrowtime(getPeriod(userBorrowPageDTO.getOper(), bl.getCreateTime(), bl.getModifyTime()));
			if("0".equals(pageDO.getOper())){//如果是在借图书,则设置图书金额
				
				if(null == bl.getBookPrice()) {
					dto.setPrice("暂无");
					dto.setDiscountPrice("暂无");
				}else {
					dto.setPrice("¥" + bl.getBookPrice().setScale(2).toString());
					dto.setDiscountPrice("¥" + bl.getBookPrice().multiply(new BigDecimal("0.7")).setScale(2, RoundingMode.HALF_UP).toString());
				}
				dto.setOrderTransId(bl.getOrderTransId());
			}
			vo.getList().add(dto);
		}
		return vo;
	}

	private String getPeriod(String oper, Date createTime, Date endTime) {
		Date end = endTime;
		if ("0".equals(oper)) {
			end = new Date();
		}
		String period = DateUtil.getDatePoor(end, createTime);
		return period;
	}

}
